package spec.chenli;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

//@WebServlet(name = "XiugServlet")
public class XiugServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = null;
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/1613580007_1?characterEncoding=u tf8", "root", "123456789");
            String sql="update t_news set title=?,content=?,publisher=? where id=?";
            PreparedStatement ps=con.prepareStatement(sql);
            Integer id=Integer.parseInt(request.getParameter("id"));
            String title=request.getParameter("title");
            String content=request.getParameter("content");
            String publisher=request.getParameter("publisher");
            ps.setString(1,title);
            ps.setString(2,content);
            ps.setString(3, publisher);
            ps.setInt(4,id);
            ps.executeUpdate();
            response.sendRedirect("listnew");
            con.close();
            ps.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}